<template>
	<view class="form-container">
		<uni-forms-item v-show="fieldConfig.name!='id'" :label="fieldConfig?.label" :name="fieldConfig?.name"
			:required="!!fieldConfig?.required" :placeholder="fieldConfig?.placeholder"
			:rules="[{ required: !!fieldConfig?.required, errorMessage: fieldConfig?.placeholder || `请输入${fieldConfig?.label}` }]">
			<uni-easyinput v-if="['input','textarea','password',].includes(fieldConfig?.type)" :type="fieldConfig?.type"
				v-model="formData[fieldConfig?.name]" trim="both" v-bind="$attrs" />
			<uni-number-box v-if="['number'].includes(fieldConfig?.type)" v-model="formData[fieldConfig?.name]"
				v-bind="$attrs" />
			<uni-data-checkbox v-if="['radio','checkbox',].includes(fieldConfig?.type)"
				v-model="formData[fieldConfig?.name]" :multiple="'checkbox' === fieldConfig?.type"
				:localdata="fieldConfig?.dictItemDatas" v-bind="$attrs" />
			<!-- <DictSelect v-if="['dict-select',].includes(fieldConfig?.type)" :dictCode="fieldConfig?.dictTypeCode" v-model="formData[fieldConfig?.name]"  /> -->
			<uni-data-select v-if="['dict-select',].includes(fieldConfig?.type)" v-model="formData[fieldConfig?.name]"
				:localdata="fieldConfig?.dictItemDatas" :clear="true" />
			<snowy-sel-picker v-if="['list-select',].includes(fieldConfig?.type)" v-model="formData[fieldConfig?.name]"
				:isMultiple="!!fieldConfig?.isMultiple" :map="'API' === fieldConfig?.selectOptionType?{
					label: 'name',
					key: 'id'
				}:{
					label: 'text',
					key: 'value'
				}" :dictCode="fieldConfig?.dictTypeCode" v-bind="$attrs" />
			<uni-datetime-picker v-if="['date',].includes(fieldConfig?.type)"
				:disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.disabled) === 'boolean' ? fieldConfig.disabled : false"
				v-model="formData[fieldConfig.name]" :type="fieldConfig?.type" return-type="string" />
			<switch
				v-if="fieldConfig && fieldConfig.type && ['switch',].includes(fieldConfig.type) && (typeof(fieldConfig.hidden) === 'boolean' ? !fieldConfig.hidden : true)"
				:disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.disabled) === 'boolean' ? fieldConfig.disabled : false"
				:checked="formData[fieldConfig.name]" @change="(e)=>{formData[fieldConfig.name] = e.detail.value}" />
			<snowy-file-picker-api
				v-if="fieldConfig && fieldConfig.type && ['uploadFile',].includes(fieldConfig.type) && (typeof(fieldConfig.hidden) === 'boolean' ? !fieldConfig.hidden : true)"
				:disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.disabled) === 'boolean' ? fieldConfig.disabled : false"
				idDataType="objArr" v-model="formData[fieldConfig.name]" />
			<snowy-user-picker
				v-if="fieldConfig && fieldConfig.type && ['userSelector',].includes(fieldConfig.type) && (typeof(fieldConfig.hidden) === 'boolean' ? !fieldConfig.hidden : true)"
				:disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.disabled) === 'boolean' ? fieldConfig.disabled : false"
				v-model="formData[fieldConfig.name]" :isMultiple="fieldConfig.isMultiple"
				:map="{key: 'id', label: 'name'}" @getOptData="selectorFunction.user.getOptData" :isPage="true"
				@getSelData="selectorFunction.user.getSelData" />
			<snowy-sel-picker v-if="['pageSelect',].includes(fieldConfig?.type)" :label="fieldConfig?.label"
				v-model="formData[fieldConfig?.name]" :isMultiple="false" :map="{
					label: 'name',
					key: 'id'
				}" :isPage="true" @getOptData="getOptData" @getSelData="getSelData" v-bind="$attrs" />
			<snowy-tree-picker v-if="['selectSupplier',].includes(fieldConfig?.type)"
				v-model="formData[fieldConfig?.name]" @getTreeOptData="getSupplierTreeOpt"
				:dictCode="fieldConfig?.dictTypeCode"
				:map="{key: 'id', parentKey: 'parentId', children: 'children', label: 'name'}" v-bind="$attrs" />
			<!-- <snowy-tree-picker v-if="['selectCustomer',].includes(fieldConfig?.type)"
				v-model="formData[fieldConfig?.name]" 
				@getTreeOptData="getCustomerTreeOpt" :dictCode="fieldConfig?.dictTypeCode"
				:map="{key: 'id', parentKey: 'parentId', children: 'children', label: 'name'}" v-bind="$attrs" /> -->
		</uni-forms-item>
	</view>
</template>
<script setup>
	import request from '@/utils/request'
	import DictSelect from '@/components/dict-select/index.vue';
	import {
		ref,
		reactive,
		onMounted
	} from "vue"
	import CallbackState from "@/enum/callback-state"
	const props = defineProps({
		fieldConfig: {
			type: Object,
			required: true
		},
		formData: {
			type: Object,
			required: true
		},
		fieldConfigDisable: {
			type: Boolean,
			default: null,
			required: false
		},
	})
	props.formData[props.fieldConfig.name] = props.fieldConfig.defalutValue
	//const options = uni.$snowy.tool.dictList(props.fieldConfig?.dictTypeCode)

	const options = async (param, callback) => {
		if (['radio', 'checkbox', ].includes(props.fieldConfig?.type)) {
			const data = await uni.$snowy.tool.dictList(props.fieldConfig?.dictTypeCode)
			return data;
		}
	}
	const getOptData = async (param, callback) => {
		if (['select', ].includes(props.fieldConfig?.type)) {
			const data = await uni.$snowy.tool.dictList(props.fieldConfig?.dictTypeCode)
			return callback({
				state: CallbackState.SUCCESS,
				data: data
			})
		} else if (['pageSelect', ].includes(props.fieldConfig?.type)) {
			const parameter = {
				current: param.pageNo,
				size: param.pageSize
			}
			request({
				url: props.fieldConfig?.selOptionApiUrl,
				method: 'GET',
				data: parameter
			}).then(data => {
				return callback({
					state: CallbackState.SUCCESS,
					data: data?.records
				})
			})
		}
	}

	const getCustomerTreeOpt = (param, callback) => bizPositionApi.positionOrgTreeSelector(param).then((data) => callback({
		state: CallbackState.SUCCESS,
		treeData: data
	}))

	const getSupplierTreeOpt = (param, callback) => bizPositionApi.positionOrgTreeSelector(param).then((data) => callback({
		state: CallbackState.SUCCESS,
		treeData: data
	}))

	// 根据已选择的数据查询数据
	const getSelData = (curSelDataKey, callback) => {
		if (uni.$snowy.tool.isNotEmpty(curSelDataKey)) {
			request({
				url: props.fieldConfig?.selDataApiUrl,
				method: 'POST',
				data: {
					idList: [curSelDataKey]
				}
			}).then(data => {
				callback(data[0])
			})
		}
	}

	// 传递用户选择器需要的API--->复制自snowy-form-item.vue
	const selectorFunction = {
		// 用户
		user: {
			getOptData: async ({
				pageNo,
				pageSize,
				keyword
			}, callback) => {
				const parameter = {
					current: pageNo,
					size: pageSize,
					searchKey: keyword
				}
				const data = await processApi.processUserSelector(parameter)
				callback({
					state: CallbackState.SUCCESS,
					data: data?.records
				})
			},
			getSelData: async (curSelDataKey, callback) => {
				if (uni.$snowy.tool.isNotEmpty(curSelDataKey)) {
					if (!props.fieldConfig.isMultiple) {
						const data = await userCenterApi.userCenterGetUserListByIdList({
							idList: [curSelDataKey]
						})
						callback({
							state: CallbackState.SUCCESS,
							data: data[0]
						})
						return
					}
					if (!!props.fieldConfig.isMultiple) {
						const data = await userCenterApi.userCenterGetUserListByIdList({
							idList: curSelDataKey
						})
						callback({
							state: CallbackState.SUCCESS,
							data: data
						})
						return
					}

				}
			}
		},
	}
</script>

<style scoped>
	/* .form-container {
		padding: 20px;
	} */
</style>